#!/usr/bin/osascript

-- Script pour créer des applications séparatrices permanentes
-- À utiliser avec Platypus

on run argv
	if (count of argv) is 0 then
		-- Affichage du menu
		return "Ajouter petit espace au dock
Ajouter grand espace au dock
----
Ajouter séparateur vertical au dock
Ajouter séparateur horizontal au dock
----
Redémarrer le Dock
À propos"
	else
		-- Action sélectionnée
		set selectedAction to item 1 of argv
		
		if selectedAction is "Ajouter petit espace au dock" then
			addSpacers("small-spacer-tile")
		else if selectedAction is "Ajouter grand espace au dock" then
			addSpacers("spacer-tile")
		else if selectedAction is "Ajouter séparateur vertical au dock" then
			createSeparatorApp("vertical")
		else if selectedAction is "Ajouter séparateur horizontal au dock" then
			createSeparatorApp("horizontal")
		else if selectedAction is "Redémarrer le Dock" then
			restartDock()
		else if selectedAction is "À propos" then
			showAbout()
		end if
	end if
end run

-- Fonction pour créer une application séparateur permanente
on createSeparatorApp(separatorType)
	try
		-- Demander le nombre de séparateurs à ajouter
		set nbSeparateurs to text returned of (display dialog "Combien de séparateurs voulez-vous ajouter au Dock ?" default answer "1" with title "Dock Spacer")
		set nbSeparateurs to nbSeparateurs as integer
		
		if nbSeparateurs < 1 then
			display dialog "Le nombre doit être supérieur à 0." buttons {"OK"} default button 1 with title "Erreur"
			return
		end if
		
		-- Créer l'application dans un dossier temporaire
		set tempDir to (path to temporary items folder as string) & "DockSeparators:"
		do shell script "mkdir -p " & quoted form of POSIX path of tempDir
		
		-- Créer le nom de l'application unique
		set appName to "DockSeparator" & separatorType & "_" & (random number from 1000 to 9999)
		set appPath to tempDir & appName & ".app"
		
		-- Créer la structure de l'application
		set appCreated to createAppBundle(appPath, appName, separatorType)
		
		if appCreated then
			-- Ajouter l'application au Dock le nombre de fois demandé
			repeat nbSeparateurs times
				addAppToDock(appPath)
			end repeat
			
			-- Redémarrer le Dock pour appliquer les changements
			do shell script "killall Dock"
			
			-- Notification de succès
			set typeText to "vertical"
			if separatorType is "horizontal" then set typeText to "horizontal"
			
			display notification (nbSeparateurs as string) & " séparateur(s) " & typeText & " ajouté(s) au Dock" with title "Dock Spacer" subtitle "Opération réussie"
		end if
		
	on error errorMessage
		-- Gestion silencieuse de l'annulation utilisateur
		if errorMessage contains "User canceled" or errorMessage contains "Annulé par l'utilisateur" then
			return
		end if
		-- Affichage uniquement des erreurs système importantes
		if errorMessage does not contain "canceled" and errorMessage does not contain "Annulé" then
			display dialog "Impossible de créer le séparateur." buttons {"OK"} default button 1 with title "Erreur"
		end if
	end try
end createSeparatorApp

-- Fonction pour ajouter une application au Dock
on addAppToDock(appPath)
	try
		set posixPath to POSIX path of appPath
		do shell script "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & posixPath & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
		return true
	on error
		return false
	end try
end addAppToDock

-- Fonction pour créer la structure complète de l'application
on createAppBundle(appPath, appName, separatorType)
	try
		set contentsPath to appPath & ":Contents"
		set macOSPath to contentsPath & ":MacOS"
		set resourcesPath to contentsPath & ":Resources"
		
		-- Créer la structure des dossiers
		do shell script "mkdir -p " & quoted form of POSIX path of macOSPath
		do shell script "mkdir -p " & quoted form of POSIX path of resourcesPath
		
		-- Créer l'exécutable principal
		set execCreated to createExecutable(macOSPath, appName)
		
		-- Copier l'icône appropriée
		copyIconToApp(resourcesPath, separatorType)
		
		-- Créer le Info.plist
		set plistCreated to createInfoPlist(contentsPath, appName, separatorType)
		
		return (execCreated and plistCreated)
		
	on error
		return false
	end try
end createAppBundle

-- Fonction pour créer l'exécutable
on createExecutable(macOSPath, appName)
	try
		set executablePath to macOSPath & ":" & appName
		
		-- Créer un script shell qui ne fait rien
		set executableContent to "#!/bin/bash
# Application séparateur pour Dock macOS
# Ne fait rien - sert uniquement de séparateur visuel
exit 0"
		
		-- Écrire le fichier
		do shell script "echo " & quoted form of executableContent & " > " & quoted form of POSIX path of executablePath
		
		-- Rendre exécutable
		do shell script "chmod +x " & quoted form of POSIX path of executablePath
		
		return true
		
	on error
		return false
	end try
end createExecutable

-- Fonction pour copier l'icône
on copyIconToApp(resourcesPath, separatorType)
	try
		set iconDestination to resourcesPath & ":AppIcon.icns"
		
		-- Obtenir le chemin des ressources de l'application actuelle
		set sourceResourcesPath to getResourcesPath()
		
		-- Choisir l'icône selon le type
		if separatorType is "vertical" then
			set iconSource to sourceResourcesPath & "/vertical.icns"
		else
			set iconSource to sourceResourcesPath & "/horizontal.icns"
		end if
		
		-- Copier l'icône
		do shell script "cp " & quoted form of iconSource & " " & quoted form of POSIX path of iconDestination
		
	on error
		-- Si la copie échoue, utiliser une icône système par défaut
		try
			do shell script "cp /System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns " & quoted form of POSIX path of iconDestination
		on error
			-- Création d'une icône vide minimale
			do shell script "touch " & quoted form of POSIX path of iconDestination
		end try
	end try
end copyIconToApp

-- Fonction pour créer le Info.plist
on createInfoPlist(contentsPath, appName, separatorType)
	try
		set plistPath to contentsPath & ":Info.plist"
		
		-- Déterminer le caractère d'affichage
		set displayChar to "│"
		if separatorType is "horizontal" then
			set displayChar to "─"
		end if
		
		-- Créer le contenu du plist
		set plistContent to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
	<key>CFBundleExecutable</key>
	<string>" & appName & "</string>
	<key>CFBundleIconFile</key>
	<string>AppIcon</string>
	<key>CFBundleIdentifier</key>
	<string>com.hybrisstudio.dockseparator." & (random number from 1000 to 9999) & "</string>
	<key>CFBundleName</key>
	<string>" & displayChar & "</string>
	<key>CFBundleDisplayName</key>
	<string>" & displayChar & "</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleVersion</key>
	<string>1.0</string>
	<key>CFBundleShortVersionString</key>
	<string>1.0</string>
	<key>LSUIElement</key>
	<true/>
	<key>NSHighResolutionCapable</key>
	<true/>
	<key>LSApplicationCategoryType</key>
	<string>public.app-category.utilities</string>
</dict>
</plist>"
		
		-- Écrire le fichier plist
		do shell script "echo " & quoted form of plistContent & " > " & quoted form of POSIX path of plistPath
		
		return true
		
	on error
		return false
	end try
end createInfoPlist

-- Fonction pour obtenir le chemin des ressources
on getResourcesPath()
	try
		set resourcesFolder to path to resource ""
		return POSIX path of resourcesFolder
	on error
		try
			set appPath to path to me as string
			set posixAppPath to POSIX path of appPath
			set bundlePath to do shell script "echo " & quoted form of posixAppPath & " | sed 's|/Contents/Resources/script$||'"
			return bundlePath & "/Contents/Resources"
		on error
			return "/tmp"
		end try
	end try
end getResourcesPath

-- Fonction pour ajouter des espaceurs (fonction originale)
on addSpacers(tileType)
	try
		set nbEspaces to text returned of (display dialog "Combien d'espaces voulez-vous ajouter ?" default answer "1" with title "Dock Spacer")
		set nbEspaces to nbEspaces as integer
		
		if nbEspaces < 1 then
			return
		end if
		
		-- Ajout des espaceurs
		repeat nbEspaces times
			do shell script "defaults write com.apple.dock persistent-apps -array-add '{\"tile-type\"=\"" & tileType & "\";}'"
		end repeat
		
		-- Redémarrage du Dock (nécessaire pour les espaceurs invisibles)
		do shell script "killall Dock"
		
		-- Notification de succès
		set spacerTypeText to "petits"
		if tileType is "spacer-tile" then set spacerTypeText to "grands"
		
		display notification (nbEspaces as string) & " " & spacerTypeText & " espace(s) ajouté(s) au Dock" with title "Dock Spacer" subtitle "Ajout terminé"
		
	on error errorMessage
		-- Gestion silencieuse de l'annulation utilisateur
		if errorMessage contains "User canceled" or errorMessage contains "Annulé par l'utilisateur" then
			return
		end if
	end try
end addSpacers

-- Fonction pour redémarrer le Dock
on restartDock()
	try
		do shell script "killall Dock"
		display notification "Le Dock a été redémarré" with title "Dock Spacer" subtitle "Terminé"
	on error
		-- Gestion silencieuse des erreurs
	end try
end restartDock

-- Fonction À propos
on showAbout()
	display dialog "Dock Spacer v2.0 - Gestion du dock macOS

Créé par Hybris Studio srl
https://www.hybris-studio.be

SÉPARATEURS VISUELS :

• Séparateur vertical au dock : pour dock horizontal
• Séparateur horizontal au dock : pour dock vertical

ESPACEURS INVISIBLES :

• Petit espace : espaceur fin invisible
• Grand espace : espaceur large invisible

UTILISATION :

1. Choisissez le type de séparateur selon votre dock
2. Indiquez combien vous en voulez
3. Ils s'ajoutent automatiquement sans redémarrer le Dock !

Astuce : Vous pouvez glisser-déposer les espaceurs pour les repositionner dans le Dock !" buttons {"OK"} default button 1 with title "À propos de Dock Spacer"
end showAbout